<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class History extends MY_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->load->model('history_model');	
	}
	
	function form_contact($contact_id)
	{
		$data['history'] = '';
		$data['id'] = $contact_id;
		$data['form_action'] = 'history/add_contact_history/';
		$this->load->view('history/form', $data);
	}
	
	public function view_contact_history($id)
	{
		$result = $this->history_model->where('contact_id',$id)->find_all();
		//print_r($this->db->last_query());
		$this->log('History', 'View History for Contact with ID: '.$id.' : '.$this->session->userdata['ip_address']);
		die(json_encode($result));
	}
	
	public function add_contact_history()
	{
		$form['user_id'] 	= $this->session->userdata['user_id'];
		$form['contact_id'] = $this->input->post('id');
		$form['note'] 		= $this->input->post('note');
		$form['date'] 		= date("Y-m-d H:i:s");
		
		if($inserted_id = $this->history_model->insert($form))
			{
				$data['error']= false;
				$data['msg']  = '<span style="color:green">History Has Been Added Successfully<br>Thanks You !</span>';
				$this->log('History', 'Create History (ID: '.$inserted_id.' ) for Contact with ID: '.$form['contact_id'].' : '.$this->session->userdata['ip_address']);
			}
			else{
				$data['error']= true;
				$data['msg']  = 'Data Base Error <br> ERROR: his/ins/100';
			}
			
		die(json_encode($data));
	}
}

